﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LevelEditor
{
    public partial class TextureEditor : Form
    {
        /// <summary>
        /// The list of rules the terrain must follow when textureing
        /// </summary>
        BindingList<TerrainRule> terrainRules
        {
            get { return CurrentMaterial.rules; }
        }

        public string MaterialName
        {
            get { return textBox1.Text; }

            set 
            { 
                textBox1.Text = value;
                CurrentMaterial.name = value;
            }
        }

        private TerrainMaterial CurrentMaterial;
        
        public TextureEditor(TerrainMaterial material)
        {
            InitializeComponent();
            CurrentMaterial = material;
            if (material.texture != null)
            {
                pictureBox1.Image = Globals.BitmapFromTexture2D(material.texture);
                pictureBox1.Size = pictureBox1.Image.Size;
            }

            MaterialName = material.name;
            FormClosing += TextureEditor_FormClosing;
            terrainRuleListBox.DataSource = terrainRules;
        }

        void TextureEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            CurrentMaterial.name = textBox1.Text;
            CurrentMaterial.texture = Globals.Texture2DFromBitmap(pictureBox1.Image as Bitmap);
            MainMenu.instance.viewWindow1.terrain.UpdateVertecies();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Material Files | *.mat";
            DialogResult result = dialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                Bitmap image = (Bitmap)MatLoader.Load(dialog.FileName);
                pictureBox1.Image = image;
                pictureBox1.Size = image.Size;
            }
        }

        private void addRuleButton_Click(object sender, EventArgs e)
        {
            TerrainRule rule = new TerrainRule(RuleType.AboveAltitude, 0.0f);
            TerrainRuleEditor ruleEditor = new TerrainRuleEditor(rule);
            terrainRules.Add(rule);
            ruleEditor.Show(this);
            ruleEditor.FormClosing += ruleEditor_FormClosing;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (terrainRuleListBox.SelectedItem == null)
                return;

            TerrainRuleEditor ruleEditor = new TerrainRuleEditor(terrainRuleListBox.SelectedItem as TerrainRule);
            ruleEditor.Show();
            ruleEditor.FormClosing += ruleEditor_FormClosing;
        }

        void ruleEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            terrainRules.ResetBindings();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (terrainRuleListBox.SelectedIndex == -1)
                return;

            terrainRules.Remove(terrainRuleListBox.SelectedItem as TerrainRule);
        }

        private void pictureBox1_SizeChanged(object sender, EventArgs e)
        {
            CurrentMaterial.texture = Globals.Texture2DFromBitmap(pictureBox1.Image as Bitmap);
        }        
    }
}
